function companyCreate(companyName)
{
	return {
		name          = companyName
		victims_flood = 0
		victims_train = 0
		victims_road  = 0
		victims_plane = 0
		victims_total = 0
		active        = true
	};
}

function companyCompare(a,b)
{
	if (a.victims_total > b.victims_total) return 1
	else
	if (a.victims_total < b.victims_total) return -1
	return 0;
}

function scoreTableCreate()
{
	return {
		active_company_score_table   = {}
		inactive_company_score_array = []
		total_victims                = 0
	};
}

function scoreTableAddCompany(scoreTable, companyId)
{
	scoreTable.active_company_score_table[companyId] <- companyCreate(GSCompany.GetName(companyId));
	GSLog.Info("Added company: " + companyId);

	return scoreTable;
}

function scoreTableIncreaseScore(scoreTable, companyId, victim_count, reason)
{
	switch(reason) {
		case GSEventVehicleCrashed.CRASH_TRAIN:
			scoreTable.active_company_score_table[companyId].victims_train += victim_count; break;
		case GSEventVehicleCrashed.CRASH_RV_UFO:
		case GSEventVehicleCrashed.CRASH_RV_LEVEL_CROSSING:
			scoreTable.active_company_score_table[companyId].victims_road += victim_count; break;
		case GSEventVehicleCrashed.CRASH_AIRCRAFT_NO_AIRPORT:
		case GSEventVehicleCrashed.CRASH_PLANE_LANDING:
			scoreTable.active_company_score_table[companyId].victims_plane += victim_count; break;
		case GSEventVehicleCrashed.CRASH_FLOODED:
			scoreTable.active_company_score_table[companyId].victims_flood += victim_count; break;
	}

	scoreTable.active_company_score_table[companyId].victims_total += victim_count;
	scoreTable.total_victims += victim_count;
	GSLog.Info("Increased score for " + companyId);

	return scoreTable;
}

function scoreTableMergeCompanies(scoreTable, buyerId, sellerId)
{
	scoreTable.active_company_score_table[buyerId].victims_flood +=
		scoreTable.active_company_score_table[sellerId].victims_flood;
	scoreTable.active_company_score_table[buyerId].victims_train +=
		scoreTable.active_company_score_table[sellerId].victims_train;
	scoreTable.active_company_score_table[buyerId].victims_road  +=
		scoreTable.active_company_score_table[sellerId].victims_road;
	scoreTable.active_company_score_table[buyerId].victims_plane +=
		scoreTable.active_company_score_table[sellerId].victims_plane;
	scoreTable.active_company_score_table[buyerId].victims_total +=
		scoreTable.active_company_score_table[sellerId].victims_total;
	delete scoreTable.active_company_score_table[companyId];

	return scoreTable;
}

function scoreTableRetireCompany(scoreTable, companyId)
{
	local company = delete scoreTable.active_company_score_table[companyId];
	company.active = false;
	scoreTable.inactive_company_score_array.append(company);
	GSLog.Info("Retired company with id: " + companyId);

	return scoreTable;
}

function scoreTableRenameCompany(scoreTable, companyId, companyName)
{
	GSLog.Info("Company " + scoreTable.active_company_score_table[companyId] + " is changing name.");
	scoreTable.active_company_score_table[companyId].name = companyName;
	GSLog.Info("Company changed name to: " + companyName);

	return scoreTable;
}

function scoreTableGetScoreTableAsList(scoreTable)
{
	local fullCompanyList = [];
	foreach(index, company in scoreTable.active_company_score_table)
	{
		fullCompanyList.append(company);
	}

	fullCompanyList.extend(scoreTable.inactive_company_score_array);
	fullCompanyList.sort(companyCompare);
	fullCompanyList.reverse();

	return fullCompanyList;
}
